package com.apes.framework.rop.session.store;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.ApesBean;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.jpa.tuple.date.DateTime;
import com.apes.framework.plugin.outbox.annotation.OutBoxExclude;
import com.apes.framework.util.DateUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenerationTime;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

@OutBoxExclude
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ApesSession.CONST_TABLE_NAME)
public class ApesSession implements ApesBean {
    public static final String CONST_TABLE_NAME = "ir_session";

    @Id
    @Column(name = "id", length = 60)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "会话")
    @Comment("会话ID")
    private String id;

    @Column(name = "ip", length = 30)
    @Comment("ip")
    private String ip;

    @Column(name = "user_id", length = 30)
    @Comment("用户Id")
    private String userId;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @DateTime(GenerationTime.INSERT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "login_on", updatable = false)
    @Comment("登陆时间")
    private Date loginOn;

    @Column(name = "logout_on", insertable = false)
    @Comment("退出时间")
    private Date logoutOn;

    @Version
    @Column(name = "version", columnDefinition = "number(10) default 0")
    @Comment("版本号")
    private int version;  //JPA乐观锁

}
